package com.lianniu.mgr.teaProduct.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.teaProduct.model.TeaSearch;
import com.lianniu.mgr.teaProduct.service.ITeaProductService;
import com.lianniu.mgr.util.TeaContant;

@Controller
@RequestMapping("/rest/tea/product")
public class TeaProductRestController {

	private final static Logger logger = Logger.getLogger(TeaProductRestController.class);

	@Autowired
	private ITeaProductService teaProductService;

	/**
	 * 获取公司发布的茶产品
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getTeaProductList", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult getTeaProductList(HttpServletRequest req) {
		logger.info("TeaProductRestController  getTeaProductList begin.");
		try {
			logger.info("getTeaProductList begin.");
			String type = req.getParameter("type");
			HashMap<String,String> param = new HashMap<String,String>();
			param.put("type", type);
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("getTeaProductList end.");
			logger.info("TeaProductRestController getTeaProductList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaProductList error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaProductList end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 获取用户发布的茶园信息列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getTeaGardenList", method = RequestMethod.GET)
	public @ResponseBody BaseResult getTeaGardenList(HttpServletRequest req) {
		logger.info("TeaProductRestController  getTeaGardenList begin.");
		try {
			logger.info("getTeaGardenList begin.");
			String index = req.getParameter("index");
			HashMap<String,Object> param = new HashMap<String,Object>();
			param.put("type", TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_GARDEN);
			if (index != null) {
				param.put("startNum", 0);
				param.put("endNum", 20);
			}
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("getTeaGardenList end.");
			logger.info("TeaProductRestController getTeaGardenList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaGardenList error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaGardenList end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 获取用户发布的茶人茶艺表演列表    
	 * @return
	 */
	@RequestMapping(value = "/getTeaShowList", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult getTeaShowList(HttpServletRequest req) {
		logger.info("TeaProductRestController  getTeaShowList begin.");
		try {
			logger.info("getTeaShowList begin.");
			String sex = req.getParameter("sex");
			String age = req.getParameter("age");
			String price = req.getParameter("price");
			String hire = req.getParameter("hire");
			String proType = req.getParameter("proType");
			String prov = req.getParameter("prov");
			String city = req.getParameter("city");
			String area = req.getParameter("area");
			Map<String,String> search = TeaSearch.getSearch(sex, age, price, hire, proType, prov, city, area);
			
			String column = req.getParameter("column");
			String sort = req.getParameter("sort");
			search.put("column", column);
			search.put("sort", sort);
			search.put("type",TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_SKILL);;
			
			BaseResult baseResult = teaProductService.getTeaProductList(search);
			logger.info("getTeaShowList end.");
			logger.info("TeaProductRestController getTeaShowList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaShowList error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaShowList end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 获取热门推荐列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getTeaHotList", method = RequestMethod.POST)
	public @ResponseBody BaseResult getTeaHotList() {
		logger.info("TeaProductRestController  getTeaHotList begin.");
		try {
			logger.info("getTeaHotList begin.");
			HashMap<String,Object> param = new HashMap<String,Object>();
			param.put("type", TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_GARDEN);
			param.put("startNum", 0);
			param.put("endNum", 3);
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("getTeaHotList end.");
			logger.info("TeaProductRestController getTeaHotList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaProductList error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaProductList end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 获取茶旅联盟列表
	 * 
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/getTeaClubList", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult getTeaClubList() {
		logger.info("TeaProductRestController  getTeaClubList begin.");
		try {
			logger.info("getTeaClubList begin.");
			HashMap<String,String> param = new HashMap<String,String>();
			param.put("type", TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_CLUB);
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("getTeaClubList end.");
			logger.info("TeaProductRestController getTeaClubList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaClubList error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaClubList end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 获取猜你喜欢列表
	 * 
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/getLikeProduct", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult getLikeProduct(HttpServletRequest req) {
		logger.info("TeaProductRestController  getLikeProduct begin.");
		try {
			logger.info("getLikeProduct begin.");
			String type = req.getParameter("type");
			HashMap<String,Object> param = new HashMap<String,Object>();
			param.put("type", type);
			param.put("startNum", 0);
			param.put("endNum", 2);
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("getLikeProduct end.");
			logger.info("TeaProductRestController getLikeProduct end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getLikeProduct error:" + e.getMessage());
			logger.error("TeaProductRestController getLikeProduct end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 搜索
	 * 
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/searchProduct", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult searchProduct(HttpServletRequest req) {
		logger.info("TeaProductRestController  searchProduct begin.");
		try {
			logger.info("searchProduct begin.");
			String type = req.getParameter("type");
			String searchCondition = req.getParameter("searchCondtion");
			HashMap<String,String> param = new HashMap<String,String>();
			param.put("type", type);
			param.put("searchCondition", searchCondition);
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("searchProduct end.");
			logger.info("TeaProductRestController searchProduct end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController searchProduct error:" + e.getMessage());
			logger.error("TeaProductRestController searchProduct end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 获取共交易的茶园总量
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getTeaCount", method = RequestMethod.GET)
	public @ResponseBody BaseResult getTeaCount() {
		logger.info("TeaProductRestController  getTeaCount begin.");
		try {
			logger.info("getTeaCount begin.");
			BaseResult baseResult = teaProductService.getTeaCount();
			logger.info("getTeaCount end.");
			logger.info("TeaProductRestController getTeaCount end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaCount error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaCount end.");
			return BaseResult.exception(e.getMessage());
		}
	}

	/**
	 * 根据商品id获取当前商品的详细信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getTeaProductInfo", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult getTeaProductInfo(HttpServletRequest req) {
		
		logger.info("TeaProductRestController  getTeaProductInfo begin.");
		try {
			String product = req.getParameter("productId");
			
			Long productId = null;
			if (product != null) {
				productId = Long.valueOf(product);
			}
			logger.info("productId:" + productId);
			logger.info("productCateList begin.");
			BaseResult baseResult = teaProductService.getTeaProductInfo(productId);
			logger.info("productCateList end.");
			logger.info("TeaProductRestController getTeaProductInfo end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getTeaProductInfo error:" + e.getMessage());
			logger.error("TeaProductRestController getTeaProductInfo end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 我的发布的查询
	 * @param req
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/getMyRelease", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult getMyRelease(HttpServletRequest req) {
		logger.info("TeaProductRestController  getMyRelease begin.");
		try {
			logger.info("getMyRelease begin.");
			//获取页面传来的参数
			String typePage = req.getParameter("type");
			String userId = req.getParameter("userId");
			HashMap<String,String> param = new HashMap<String,String>();
			String type = null;
			if("0".equals(typePage)){
				type = "2";
			}else if("1".equals(typePage)){
				type = "3";
			}else if("2".equals(typePage)){
				type = "4";
			}else if("3".equals(typePage)){
				type = "1";
			}else if("4".equals(typePage)){
				type = "6";
			}else if("5".equals(typePage)){
				type = "7";
			}else if("9".equals(typePage)){
				type = "9";
			}
			param.put("type", type);
			param.put("userId", userId);
			BaseResult baseResult = teaProductService.getTeaProductList(param);
			logger.info("getMyRelease end.");
			logger.info("TeaProductRestController getMyRelease end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getMyRelease error:" + e.getMessage());
			logger.error("TeaProductRestController getMyRelease end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
	/**
	 * 我的收藏的查询
	 * @param req
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/getMyCollection", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody BaseResult getMyCollection(HttpServletRequest req) {
		logger.info("TeaProductRestController  getMyCollection begin.");
		try {
			logger.info("getMyCollection begin.");
			//获取页面传来的参数
			String typePage = req.getParameter("type");
			String userId = req.getParameter("userId");
			HashMap<String,String> param = new HashMap<String,String>();
			String type = null;
			if("0".equals(typePage)){
				type = "2";
			}else if("1".equals(typePage)){
				type = "3";
			}else if("2".equals(typePage)){
				type = "4";
			}else if("3".equals(typePage)){
				type = "6";
			}
			param.put("type", type);
			param.put("userId", userId);
			BaseResult baseResult = teaProductService.getTeaProductListByCollection(param);
			logger.info("getMyCollection end.");
			logger.info("TeaProductRestController getMyCollection end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaProductRestController getMyCollection error:" + e.getMessage());
			logger.error("TeaProductRestController getMyCollection end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
}
